package snake;

import java.awt.*;
import java.util.List;

/**
 * User: luotao
 * Date: 11-5-5
 */
public class SnakePoint extends Point {
    public SnakeStatus currentStatus;

    private MainCanvas mainCanvas;

    public String name;

    public SnakePoint(int x, int y, MainCanvas mainCanvas) {
        super(x, y);
        this.mainCanvas = mainCanvas;
    }

    /**
     * 检查是否超出了游戏区域
     *
     * @return
     */
    public boolean checkOutBound() {
        switch (currentStatus) {
            case RIGHT:
                return x > GameSetting.GAME_AREA_MAX_X / 10;
            case LEFT:
                return x < 0;
            case UP:
                return y < 0;
            case DOWN:
                return y > GameSetting.GAME_AREA_MAX_Y / 10;
            default:
                return false;
        }
    }

    public void move() {
        switch (currentStatus) {
            case UP:
                y--;
                break;
            case DOWN:
                y++;
                break;
            case RIGHT:
                x++;
                break;
            case LEFT:
                x--;
                break;
        }
    }

    public void moveTurn(List<SnakePoint> turns) {
        if (turns.size() > 0) {
            for (int i = 0; i < turns.size(); i++) {
                SnakePoint turn = turns.get(i);
                switch (turn.currentStatus) {
                    case DOWN:
                        if (x == turn.x && y == turn.y) {
                            currentStatus = turn.currentStatus;
                        }
                        break;
                    case UP:
                        if (x == turn.x && y == turn.y) {
                            currentStatus = turn.currentStatus;
                        }
                        break;
                    case LEFT:
                        if (y == turn.y && x == turn.x) {
                            currentStatus = turn.currentStatus;
                        }
                        break;
                    case RIGHT:
                        if (y == turn.y && x == turn.x) {
                            currentStatus = turn.currentStatus;
                        }
                        break;
                    default:
                        System.out.println("out");
                        break;
                }
            }
            if (mainCanvas.snakeList.indexOf(this)
                    == mainCanvas.snakeList.size() - 1) {
                SnakePoint lastTurn = turns.get(0);
                if (lastTurn.x == x && lastTurn.y == y) {
                    turns.remove(lastTurn);
                }
            }
        }
        move();
    }
}
